package com.atguigu.jxc.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.DamageListGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * User: fqy
 * Date: 2021/10/18
 * Time: 13:22
 * Describe: 商品报损控制器
 */
@RestController
@RequestMapping("damageListGoods")
public class DamageListGoodsController {

    @Autowired
    private DamageListGoodService damageListGoodService;

    //http://localhost:8080/damageListGoods/save?damageNumber=BS1634536130619

    /**
     * 保存报损的商品
     * @param
     * @return
     */
    @PostMapping("save")
    public ServiceVO saveDamageListGoods(DamageList damageList, String damageListGoodsStr, HttpSession session){
        // 将信息保存到 t_damage_list表和t_damage_list_goods表
        User currentUser = (User) session.getAttribute("currentUser");
        damageList.setUserId(currentUser.getUserId());
        int i = damageListGoodService.saveDamageListGoods(damageList,damageListGoodsStr);
        if (i==0) {
            // 失败
            return new ServiceVO(200, "新增damageList失败");
        }
        return new ServiceVO(100, "新增damageList成功");
    }
    // 根据日期范围 和报损单类型 查询报损信息或者报益信息
    // 当用户点击报损单或报溢单信息时，下方展示单据所关联的商品信息

    /**
     * 根据日期范围查询报损单  http://localhost:8080/damageListGoods/list
     * @param sTime 开始时间
     * @param eTime 结束时间
     * @return
     */
    @PostMapping("list")
    public Map<String,Object> getDamageListByDate(String  sTime, String  eTime){
        return damageListGoodService.getDamageListByDate(sTime, eTime);
    }

    /**
     * 根据报损单Id查询报损单商品信息 http://localhost:8080/damageListGoods/goodsList
     * @param damageListId 报损单Id
     * @return
     */
    @PostMapping("goodsList")
    public Map<String,Object> getDamageListGoods(Integer damageListId){

        return damageListGoodService.getDamageListGoodsById(damageListId);
    }
}
